defmodule ProgContest.BlockingQueueTest do
  use ExUnit.Case
  alias ProgContest.BlockingQueue

  test "BlockingQueue should be FIFO" do
    BlockingQueue.push(1)
    BlockingQueue.push(2)
    BlockingQueue.push(3)
    assert 1 == BlockingQueue.pop()
    assert 2 == BlockingQueue.pop()
    assert 3 == BlockingQueue.pop()
  end

  test "BlockingQueue.pop/1 should block when the queue is empty" do
    # Spawn a process that pushes an item to the queue 6 seconds later
    spawn_link(fn->
      Process.sleep(6_000)  # sleep 6 seconds
      BlockingQueue.push("Hi")
    end)
    {time, _} = :timer.tc(fn->
      assert "Hi" == BlockingQueue.pop()
    end)
    assert time > 5_000_000  # `pop` should block more than 5 seconds (which is the default timeout for GenServer.call)
  end
end
